/* QAD_CurrentSurplusInventory.p - Report Proxy Template                      */
/* Copyright 1986-2010 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Id:: QAD_CurrentSurplusInventory.p 1046 2010-09-10 09:08:19Z jvz  $:i     */
/*                                                                            */
/* Report matches legacy ppptrp21.p for use by QAD Reporting Framework.       */
/*                                                                            */

{mfsubdirs.i}
{{&US_BBI}mfdeclre.i}
{{&US_BBI}gplabel.i}

{com/qad/shell/report/dsReportRequest.i}
{com/qad/shell/report/ReportConstants.i}

/*============================================================================*/
/* ****************************  TEMP TABLES  ******************************* */
/*============================================================================*/

/* TEMP-TABLE DEFINITION BLOCK */

define temp-table tt_pt_mstr no-undo like pt_mstr
   field site    like ld_det.ld_site
   field loc     like ld_det.ld_loc
   field stat    like ld_det.ld_status
   field issdate like in_mstr.in_iss_date
   field qty_x   like in_mstr.in_qty_oh
   field qty_1   like in_mstr.in_qty_oh
   field val_x   like glt_det.glt_amt
   field val_1   like val_x
.

define temp-table ttReportOptions no-undo
   field date1   like in_mstr.in_iss_date
   field costset like cs_mstr.cs_set
   field summary like mfc_ctrl.mfc_logical
.

define dataset dsReportResults for tt_pt_mstr, ttReportOptions.

/*============================================================================*/
/* *****************************  MAIN BLOCK  ******************************* */
/*============================================================================*/
define input parameter runReport    as logical no-undo.
define input parameter reportHandle as handle  no-undo.
define input parameter dataset      for dsReportRequest.
define output parameter dataset-handle phReportResults.

{com/qad/shell/report/reporting.i}

define variable vhDS as handle    no-undo.

empty temp-table tt_pt_mstr      no-error.
empty temp-table ttReportOptions no-error.

for first ttReportRequest no-lock:

   vhDS = dataset dsReportResults:handle.

   run FillMetaData in reportHandle(
      vhDS, "QAD_CurrentSurplusInventory.meta.xml").

   run SetReportMetaData (input reportHandle).

   if runReport then do:
      run RunReport
         (output dataset-handle phReportResults).
   end.

end.

PROCEDURE SetReportMetaData:

   define input parameter  ipReportHandle as handle.

   /*
   run SetFieldMetaParameter in ipReportHandle(
         "ttCoByProductsCostRpt", "si_site", "DefaultValue", site).
   */

END PROCEDURE. /* PROCEDURE ModifyMetaData */

/* DATA RETRIEVING LOGIC BLOCK */
PROCEDURE RunReport:

   define output parameter dataset-handle phReportResults.

   /* RETRIEVE THE DATA FROM DATABASE */
   define variable queryString   as character no-undo.
   define variable hMainQuery    as handle    no-undo.
   define query    MainQuery     for so_mstr.
   define variable pgflag  as logical no-undo.
   
   
   assign
      pgflag   =  yes
      qty_x    =  0
      qty_1    =  0
      val_x    =  0
      val_1    =  0.
      summary  =  isTrueValue("ttReportOptions",
                                   "item",
                                   false).
      


      hMainQuery           = query MainQuery:handle.
      
   queryString = "for each ld_det no-lock where ld_domain = "
                  + quoter(global_domain).

   run AddAllConditions in reportHandle
      ("so_mstr", input-output queryString).

   queryString = queryString + ":".

   hMainQuery:query-prepare(queryString).
   hMainQuery:query-open().
   hMainQuery:get-next().

   repeat while not hMainQuery:query-off-end:

      hMainQuery:get-next().
   end. /* REPEAT QUERY */

   hMainQuery:query-close().
   delete object hMainQuery.
   phReportResults = dataset dsReportResults:handle.

END PROCEDURE.
